package com.netbiscuits.tactile;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

@SuppressWarnings("serial")
public class DetailServlet extends HttpServlet {

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String id = req.getParameter("id");
		
		if(id != null && !id.trim().equals("")) {
			Map<String, News> mapNews = (Map<String, News>) req.getSession().getAttribute("mapNews");
			
			if(mapNews != null && !mapNews.isEmpty()) {
				News news = mapNews.get(id);
				
				if(news != null) {
					JSONObject obj = new JSONObject();
					obj.put("title", news.getTitle());
					obj.put("content", news.getDescription());
					obj.put("image", news.getImage());
					
					resp.setContentType("application/json");
					
					PrintWriter out = resp.getWriter();
					out.print(obj);
					out.flush();
				}				
			}					
			
		} else {
			throw new IOException("No id found");
		}
		
		
	}
	
}
